package com.mlink.vs.util;

import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import cn.jiguang.common.ClientConfig;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.InterfaceAdapter;
import cn.jpush.api.push.model.notification.Notification;
import cn.jpush.api.push.model.notification.PlatformNotification;

public class jgpush {

    protected static final String APP_KEY ="2a733c5764a8fc818e9b61a8";
    protected static final String MASTER_SECRET = "51900a9a025cf98fe3b6d328";

	
    public static void main(String[] args) {
    	     //String payloadString = "{\"platform\":{\"all\":false,\"deviceTypes\":[\"Android\"]},\"audience\":{\"all\":false,\"targets\":[{\"audienceType\":\"TAG_AND\",\"values\":[\"tag1\",\"tag_all\"]}]},\"notification\":{\"notifications\":[{\"soundDisabled\":false,\"badgeDisabled\":false,\"sound\":\"happy\",\"badge\":\"5\",\"contentAvailable\":false,\"alert\":\"Test from API Example - alert\",\"extras\":{\"from\":\"JPush\"},\"type\":\"cn.jpush.api.push.model.notification.IosNotification\"}]},\"message\":{\"msgContent\":\"Test from API Example - msgContent\"},\"options\":{\"sendno\":1429488213,\"overrideMsgId\":0,\"timeToLive\":-1,\"apnsProduction\":true,\"bigPushDuration\":0}}";
    	     //sendPush_fromJSON(payloadString);
    	    sendPush_fromRegistrationId("9999推送鹌鹑极光来啦","100d85590942fd5e0b4");
	}
    
    public static void sendPush_fromRegistrationId(String Message,String registrationId) {  
         PushPayload payload =  buildPushObject_android_cid(Message,registrationId);
         senPush(payload);
    }
	 public static int sendPush_fromRegistrationId_Message(String alertContent,String title,String registrationId, Map<String, String> extras) {  
         PushPayload payload =  buildPushObject_android_cid_Message(alertContent,title,registrationId,extras);
         int statusCode = senPush(payload);
		return statusCode;
    }
    
    
    public static void sendPush_fromJSON(String payloadString) { 
        Gson gson = new GsonBuilder()
                .registerTypeAdapter(PlatformNotification.class, new InterfaceAdapter<PlatformNotification>())
                .create();
         PushPayload payload = gson.fromJson(payloadString, PushPayload.class);
         senPush(payload);
    }
    
    
    public static int senPush(PushPayload payload) {
    	      ClientConfig clientConfig = ClientConfig.getInstance();
          JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, clientConfig); 
          int statusCode =0;
          try {
              PushResult result = jpushClient.sendPush(payload);
              System.out.println("Got result - " + result);
              statusCode = result.statusCode;
          } catch  (Exception e) {
          	   System.out.println("errors:"+e.getLocalizedMessage());
          	  	e.printStackTrace(); 
          }
		return statusCode;
    }

    /**
     * 根据registrationId 发送
     * @param Message
     * @param registrationId
     * @return
     */
    public static PushPayload buildPushObject_android_cid(String Message,String registrationId) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.registrationId(registrationId))
                .setNotification(Notification.alert(Message))
                //.setCid("cid")
                .build();
    }
    
    /**
     * 根据registrationId 发送
     * @param alertContent
     * @param title
     * @param registrationId
     * @param extras
     * @return
     */
    public static PushPayload buildPushObject_android_cid_Message(String alertContent,String title,String registrationId, Map<String, String> extras) {
       
        return PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.registrationId(registrationId))
                .setNotification(Notification.newBuilder().setAlert(alertContent).addPlatformNotification(AndroidNotification.newBuilder().setTitle(title).addExtras(extras).build()).build()) 
                .build();
    }
    
    
    
    /**
     *  根据tag，发送
     * @param message 信息
     * @param title  标题
     * @param tag   tag
     * @return
     */
    public static PushPayload buildPushObject_android_tag_alertWithTitle(String message, String title, String... tag) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.tag(tag))
                .setNotification(Notification.android(message, title, null))
                .build();
    }
    
    /**
     *  根据alias，发送
     * @param message 信息
     * @param title  标题
     * @param tag   tag
     * @return
     */
    public static PushPayload buildPushObject_android_alias_alertWithTitle(String message, String title, String... tag) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android())
                .setAudience(Audience.alias(tag))
                .setNotification(Notification.android(message, title, null))
                .build();
    }

}
